package com.group3.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
//import com.google.common.collect.Sets;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
//import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
//import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * Swagger2Config
 *
 * @author HY
 * 2021/05/21 8:15
 **/

//@Configuration
//@Slf4j
//@EnableKnife4j
//@EnableSwagger2
////@EnableSwagger2WebMvc
//@Import(BeanValidatorPluginsConfiguration.class)
//public class Swagger2Config {
//
//    private OpenApiExtensionResolver openApiExtensionResolver;
//
//    @Autowired
//    public Swagger2Config(OpenApiExtensionResolver openApiExtensionResolver) {
//        this.openApiExtensionResolver = openApiExtensionResolver;
//    }
//
//
//    @Bean
//    Docket createRestApi(Environment environment) {
////        ResponseMessage requestParameterError = new ResponseMessageBuilder().code(400).message("Request Parameter Error").build();
////        ResponseMessage notFound = new ResponseMessageBuilder().code(404).message("Not Found").build();
////        ResponseMessage internalServerError = new ResponseMessageBuilder().code(500).message("Internal Server Error").build();
//
//        //设置要显示的swagger环境:只在生产环境中使用，在发布环境中不适用！
//        Profiles profiles = Profiles.of("dev", "test");
//
//        //通过environment.acceptsProfiles判断是否启动处于设定的环境中
//        boolean flag = environment.acceptsProfiles(profiles);
//        System.out.println(flag);
//
//        return new Docket(DocumentationType.SWAGGER_2)
//                .useDefaultResponseMessages(false)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.group3.controller"))
//                .paths(PathSelectors.any())
//                .build()
//                .pathMapping("/")
//
//                // 以字符串代替日期格式显示
//                .directModelSubstitute(LocalDate.class, String.class)
//                .directModelSubstitute(LocalDateTime.class, String.class)
//                .genericModelSubstitutes(ResponseEntity.class)
//
////                footer设置
//                .extensions(openApiExtensionResolver.buildSettingExtensions())
////                自定义文档
//                .extensions(openApiExtensionResolver.buildExtensions("1.0.0"))
//
//                // 页面显示信息
//                .apiInfo(apiInfo())
//                .enable(flag);
//
////                // 设置全局自定义异常消息返回
////                .useDefaultResponseMessages(false)
////                .globalResponseMessage(RequestMethod.GET, Arrays.asList(requestParameterError, notFound, internalServerError))
////                .globalResponseMessage(RequestMethod.POST, Arrays.asList(requestParameterError, internalServerError))
////                .globalResponseMessage(RequestMethod.PUT, Arrays.asList(requestParameterError, internalServerError))
////                .globalResponseMessage(RequestMethod.OPTIONS, Arrays.asList(requestParameterError, internalServerError))
////                .globalResponseMessage(RequestMethod.DELETE, Arrays.asList(requestParameterError, internalServerError));
//    }
//    private ApiInfo apiInfo() {
//            return new ApiInfo("swagger2  API",
//            "swagger2 API 文档",
//            "1.0.0",
//            "/",
//            new Contact("swagger2 API文档", "/doc.html", ""),
//            "",
//            "",
//            new ArrayList<>());
//            }



//    3.0.2版本

@Configuration
@Slf4j
@EnableOpenApi
public class Swagger2Config implements WebMvcConfigurer {
    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public Swagger2Config(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }
    @Bean(value = "defaultApi2")
    public Docket defaultApi2(Environment environment) {
        String groupName="2.X版本";

        //设置要显示的swagger环境:只在生产环境中使用，在发布环境中不适用！
        Profiles profiles = Profiles.of("dev", "test");

        //通过environment.acceptsProfiles判断是否启动处于设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);

        Docket docket = new Docket(DocumentationType.OAS_30)
//                .host("https://www.baidu.com")
//                .consumes(Sets.newHashSet("application/json"))
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
//                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.group3.controller"))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName))
//                footer设置
                .extensions(openApiExtensionResolver.buildSettingExtensions())
//                自定义文档
                .extensions(openApiExtensionResolver.buildExtensions("1.0.0"))


                .enable(flag);
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //.title("swagger-bootstrap-ui-demo RESTful APIs")
                .description("swagger2 API 文档")
                .termsOfServiceUrl("")
                .contact(new Contact("knife4j3.0.2 API文档", "/doc.html", ""))
                .version("1.0")
                .build();
    }
}
